﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO.Ports;
using System.Windows.Threading;

namespace SerialPortLogger
{


    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        #region Private fields
        double _timeInterval = 60000;
        #endregion

        // Serial port
        private SerialPort serialPort = new SerialPort();
        const string TERM_CHAR = "\n";

        // Create a DispatcherTimer
        DispatcherTimer timer = new DispatcherTimer();

        public MainWindow()
        {
            InitializeComponent();
            Dispatcher.ShutdownStarted += Dispatcher_ShutdownStarted;
        }
        
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            // setup the serial port - TODO: this should be moved to the app config or some other configuration file.
            serialPort.BaudRate = 9600;
            serialPort.PortName = "COM3";
            serialPort.DtrEnable = true;
            serialPort.NewLine = TERM_CHAR;

            // Open the serial port
            serialPort.Open();

            // Open up a database connection
            DataLogger.OpenConnection();

            // Give the sketch chance to start up.
            System.Threading.Thread.Sleep(2000);

            // get temp once on startup then leave to the timer
            timer_Tick(null,null);

            // Call our event handler every 60 seconds
            timer.Interval = TimeSpan.FromMilliseconds(_timeInterval);

            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();
        }



        void timer_Tick(object sender, EventArgs e)
        {
            // ask for a temperature
            try
            {
                // request a temperature
                serialPort.WriteLine("get temp");

                string serialData = serialPort.ReadLine();

                // get the temperature.
                double temp = Convert.ToDouble(serialData);

                tbTempData.Text = temp.ToString();

                // log the data
                DataLogger.LogData(temp.ToString(), "temp1");


                if (temp > 25)
                {
                    tbDescription.Text = "Damn its toasty in here!";
                }
                else if (temp <= 25 && temp > 20)
                {
                    tbDescription.Text = "I think we're warm enough!";
                }
                else if (temp <= 20)
                {
                    tbDescription.Text = "It's bloody freezing in here!!!";
                }
            }
            catch (Exception) // otherwise it didn't work and we'll try again next time.
            {
                Console.WriteLine("Something didn't work. Trying again in a minute.");
            }



        }

        private void Dispatcher_ShutdownStarted(object sender, EventArgs e)
        {
            // tidy up when we're done.
            DataLogger.CloseConnection();
        }
    }
}
